export default {
	namespaced: true,
	state: {
		text: '测试文本',
		cart: uni.getStorageSync('cart') || []
	},
	mutations: {
		addToCart(state, goodsObj) {
			let obj = state.cart.find(item => item.goods_id === goodsObj.goods_id)
			if (!obj) {
				state.cart.push(goodsObj)
			} else {
				obj.goods_count += 1
			}
			uni.setStorageSync('cart', state.cart)
		},
		// 更改radio是否选中
		changeRadio(state, obj) {
			let myobj = state.cart.find(item => item.goods_id === obj.goods_id)
			if (myobj) {
				myobj.goods_status = obj.goods_status
			}
			uni.setStorageSync('cart', state.cart)
		},
		// 更改购物车数量
		changeNumber(state, obj) {
			let myobj = state.cart.find(item => item.goods_id === obj.goods_id)
			if (myobj) {
				myobj.goods_count = obj.goods_count
			}
			uni.setStorageSync('cart', state.cart)
		},
		// 删除单个购物车数据
		deleteBuyCar(state, obj) {
			state.cart = state.cart.filter(item => item.goods_id !== obj)
			uni.setStorageSync('cart', state.cart)
		},
		//更改是否全选方法
		changeStatus(state, bool) {
			state.cart.forEach(item => {
				return item.goods_status = bool
			})
			uni.setStorageSync('cart', state.cart)
		}

	},
	actions: {},
	getters: {
		//购物车选中总数量
		total(state) {
			return state.cart.reduce((sun, item) => {
				return sun += item.goods_status ? item.goods_count : 0
			}, 0)
		},
		// 是否全选
		allStatus(state) {
			return state.cart.every(item => item.goods_status)
		},
		// 计算商品总价
		totalPrice(state) {
			return state.cart.reduce((sun, item) => {
				return sun += item.goods_status ? item.goods_count * item.goods_price : 0
			}, 0)
		}
	}
}
